
<!DOCTYPE html>
<html lang="en" class="loading">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>How to understand &#39;struct&#39; in language C - Hexo</title>
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate">
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="C语言中所有的结构体定义方法写的原因：数据结构，严奶奶的书，最近找工作复习。。发现里面的结构体定义看不太懂，因为C++的结构体太好用了！

第一种定义方法是先定义结构体再定义变量名
 1234567,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="Hexo" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
    <link rel="stylesheet" href="/css/diaspora.css">
	<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script>
         (adsbygoogle = window.adsbygoogle || []).push({
              google_ad_client: "ca-pub-8691406134231910",
              enable_page_level_ads: true
         });
    </script>
    <script async custom-element="amp-auto-ads" src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js">
    </script>
</head>
</html>
<body class="loading">
    <span id="config-title" style="display:none">Hexo</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="http://yoursite.com"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">How to understand 'struct' in language C</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">How to understand 'struct' in language C</h1>
        <div class="stuff">
            <span>八月 16, 2019</span>
            
  <ul class="post-tags-list"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/C语言/">C语言;</a></li></ul>


        </div>
        <div class="content markdown">
            <h2 id="C语言中所有的结构体定义方法"><a href="#C语言中所有的结构体定义方法" class="headerlink" title="C语言中所有的结构体定义方法"></a>C语言中所有的结构体定义方法</h2><p><del>写的原因：数据结构，严奶奶的书，最近找工作复习。。发现里面的结构体定义看不太懂，因为<strong>C++的结构体太好用了</strong>！</del></p>
<ol>
<li><p>第一种定义方法是先定义结构体再定义变量名</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">struct student&#123;</span><br><span class="line">	int sno;</span><br><span class="line">	char name[50];</span><br><span class="line">	int score;</span><br><span class="line">&#125;;</span><br><span class="line">//main函数中的使用,C语言中一般还是必须的加入struct的</span><br><span class="line">struct student mea;</span><br></pre></td></tr></table></figure>
</li>
<li><p>第二种方法就是在定义结构体的时候就把变量名给定义了</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">	struct student&#123;</span><br><span class="line">	int sno;</span><br><span class="line">	char name[50];</span><br><span class="line">	int score;</span><br><span class="line">&#125;mea;</span><br><span class="line">这时候mea就作为一个变量就可以用了，神奇！</span><br></pre></td></tr></table></figure>
</li>
<li><p>第三种方法就是直接定义变量名，而不要结构体的名字，但是一般人绝对不会这么搞得吧。。。</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">struct&#123;</span><br><span class="line">int sno;</span><br><span class="line">char name[50];</span><br><span class="line">int score;</span><br><span class="line">&#125;mea;</span><br></pre></td></tr></table></figure>
</li>
<li><p>第四种就是数据结构这本书上最常出现的情况了</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">typedef struct student&#123;</span><br><span class="line">	int sno;</span><br><span class="line">	char name[50];</span><br><span class="line">	int score;</span><br><span class="line">&#125;Mea,Aqua;</span><br><span class="line">//main函数中有三种定义方式</span><br><span class="line">Mea mea;</span><br><span class="line">Aqua mea;</span><br><span class="line">struct student mea;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h2 id="你要是把这个玩意横过来-其实就对应了typedef的使用方法了只是因为有花括号所以不需要空格了。"><a href="#你要是把这个玩意横过来-其实就对应了typedef的使用方法了只是因为有花括号所以不需要空格了。" class="headerlink" title="你要是把这个玩意横过来 其实就对应了typedef的使用方法了只是因为有花括号所以不需要空格了。"></a>你要是把这个玩意横过来 其实就对应了typedef的使用方法了只是因为有花括号所以不需要空格了。</h2><h2 id="结构体的初始化。对应相应的类型"><a href="#结构体的初始化。对应相应的类型" class="headerlink" title="结构体的初始化。对应相应的类型"></a>结构体的初始化。对应相应的类型</h2><pre><code><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">struct student&#123;</span><br><span class="line">	int sno;</span><br><span class="line">	char name[50];</span><br><span class="line">	int score;</span><br><span class="line">&#125;;</span><br><span class="line">int main()&#123;</span><br><span class="line">	struct student mea = &#123;101,&quot;mea_suki&quot;,95&#125;;</span><br><span class="line">	printf(&quot;%d %s,%d&quot;,mea.sno,mea.name,mea.score);</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></code></pre><hr>
<h2 id="结构体变量定应用："><a href="#结构体变量定应用：" class="headerlink" title="结构体变量定应用："></a>结构体变量定应用：</h2><pre><code>结构体中的成员可以像普通的变量那样单独的使用，如下面例子中，结构体变量A中的成员num，可以使用A.num</code></pre><hr>
<h2 id="关于结构体的一点说明"><a href="#关于结构体的一点说明" class="headerlink" title="关于结构体的一点说明"></a>关于结构体的一点说明</h2><ol>
<li><p>不能把结构体当成一个整体进行操作<br>printf(“%d,%s ,%d”,mea);//对应上了？没听说过。。。<br>scanf(“%d, %s %d”,&amp;mea);//也不行<br>printf(“%d, %s %d”,mea.sno,mea.name,mea.score);//可以使用<br>scanf(“%d, %s %d”,&amp;mea.sno,&amp;mea.name,&amp;mea.score);//可以使用</p>
</li>
<li><p>当结构域体变量的成员也是结构体类型时，引用必须用最底层的变量：如下</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">struct Age&#123;</span><br><span class="line">	int year;</span><br><span class="line">	int month;</span><br><span class="line">	int day;</span><br><span class="line">&#125;;</span><br><span class="line">struct student&#123;</span><br><span class="line">	int sno;</span><br><span class="line">	int score;</span><br><span class="line">	struct Age age;</span><br><span class="line">&#125;;</span><br><span class="line">int main()&#123;</span><br><span class="line">	struct student mea;</span><br><span class="line">	scanf(&quot;%d&quot;,&amp;mea.age.day);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>程序中的变量名和成员名相同，二者代表不同对象</p>
</li>
<li><p>可以引用结构体变量成员地址，也可以引用结构体变量的地址<br>scanf(“%d”,&amp;mea.sno)//输入A的num值<br>printf(“%d”，&amp;mea)//输出mea的首地址</p>
</li>
<li><p>相同类型的变量可以相互赋值，其他情况不允许直接赋值</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">struct student mea;</span><br><span class="line">struct student auqa;</span><br><span class="line">mea = auqa;</span><br></pre></td></tr></table></figure>




</li>
</ol>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title='0' data-url='http://link.hhtjim.com/163/425570952.mp3'></li>
                        
                    
                        
                            <li title='1' data-url='http://link.hhtjim.com/163/425570952.mp3'></li>
                        
                    
                </ul>
            
        </div>
        
    <div id='gitalk-container' class="comment link"
		data-enable='false'
        data-ae='false'
        data-ci=''
        data-cs=''
        data-r=''
        data-o=''
        data-a=''
        data-d='false'
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>

<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/diaspora.js"></script>
<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">
<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
